import java.util.ArrayList;
import java.util.Arrays;

/**
 * @User: vitobo
 * @Date: 2024-06-20
 * @Description: 栈实现
 *
 *  一种后进先出的结构
 *
 */
public class MyStack {
    public int[] elem;
    public int usedSize;
    public static final int DEFAULT_SIZE = 10;

    public MyStack(){
        this.elem = new int[DEFAULT_SIZE];
    }

    /**
     * 压栈
     */
    public int push(int val){
        if(isFull()){
            elem = Arrays.copyOf(elem, 2*elem.length);
        }
        this.elem[usedSize++] = val;
        return val;
    }

    public boolean isFull(){
        return usedSize == elem.length;
    }

    /**
     * 弹栈
     */
    public int pop(){
        if(empty()){
            throw new MyEmptyStackException("栈为空！ ");
        }
        return elem[--usedSize];
    }

    public boolean empty() {
        return usedSize == 0;
    }

    public int peek(){
        if(empty()){
            throw new MyEmptyStackException("栈为空！ ");
        }
        return elem[usedSize-1];
    }


}
